package com.zhenyi.api;

import com.cenjiawen.common.IndexController;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.utils.PathUtils;
import com.zhenyi.core.interceptor.ExceptionInterceptor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.URLUtil;

/**
 * 共用接口
 * 
 * @author cen
 *
 */
public class ApiController extends IndexController {

	/**
	 * 生成二维码
	 */
	public void qrcode() {
		String text = getPara("text");
		int size = getParaToInt("size", 200);
		int lt = getParaToInt("lt", 0);
		if (lt == 0) {
			redirect("http://www.liantu.com/api.php?text=" + URLUtil.encode(text));
		} else {
			renderQrCode(text, size, size);
		}
	}

	@Before(ExceptionInterceptor.class)
	public void log() {
		String project = getPara("project");
		if (StrKit.isBlank(project)) {
			renderFailure("no project");
			return;
		}
		String data = getPara("data");
		if (StrKit.isBlank(project)) {
			renderFailure("no data");
			return;
		}
		String url = getPara("url", "");
		String path = PathUtils.getFile("data", "apilog", project, DateUtil.today() + ".txt");
		FileWriter fileWriter = new FileWriter(path);
		System.out.println("data :" + data);
		fileWriter.append("====" + DateUtil.now() + "=====" + url + "\n");
		fileWriter.append(data + "\n");
		renderSuccess();
	}

}
